<?php
require("bases.php");

class student_info extends bases {

	public function __construct(){
		parent::__construct();
		try{
			$this->fmenu = $this->controller = "student_info";
			$this->position_one = array("text"=>"学生管理");
		}catch(Exception $e){

		}
	}

	public function datalist(){
		$obj_student = spClass('students');
		$obj_student_info = spClass('student_info');
		$postdata = $this->spArgs();
		$page = intval(max($postdata['page'], 1));
		$condition = "1 and is_del = 0";
		if($searchkeys = $postdata['searchkeys']){
			$condition .= " and (sys_student.nickname like '%{$searchkeys}%' )";
			$this->searchkeys = $searchkeys;
		}
		$data_rs = $obj_student
			->spPager($page, 10)->findAll($condition,  'id desc');
		if($data_rs){
			foreach($data_rs as $k => $v){
				$info = $obj_student_info ->find(array('student_id' =>$v['id']));
				if($info)
					$data_rs[$k]['info'] = $info;
			}
		}
		$this->data_rs = $data_rs;
		$this->pager = $obj_student->spPager()->getPager();
		$this->url = spUrl('student', 'datalist', array("searchkeys"=>$this->searchkeys));
		$this->position_two = array("text"=>"学生管理", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生管理");
		$this->searchform = 1;
	}

	public function create(){
		$obj_student = spClass('students');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			try{
				$postdate = $this->spArgs();
				$data = array();
				//客户端提交方法
				$data['account'] = $postdate['account'];
				$data['password'] = md5($postdate['password']);
				$data['status'] = intval($postdata['status']);
				
				if(!$obj_student->create($data))
					throw new Exception("未知错误，学生添加失败");
				$url = spUrl($this->controller, "datalist");
				$message = array('msg'=>'学生添加成功！','result'=>1, 'url'=>$url);
				echo json_encode($message);
				exit();
			}catch(Exception $e){
				$message = array('msg'=>$e->getMessage() ,'result'=>-1);
				echo json_encode($message);
				exit();
			}
		}
		$condition = "1";
		// $this->validator = $obj_user->getValidatorJS();
		$this->info_rs = $obj_student->findAll($condition);
		$this->saveurl = spUrl("student", "create");
		$this->position_two = array("text"=>"学生添加", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生添加");
	}

	function modify(){
		try {
			$postdata = $this->spArgs();
			$obj_student = spClass("students");
			if(!$id = intval($postdata['id']))
				throw new Exception("非法操作");
			if(!$info_rs = $obj_student->find(array("id"=>$id)))
				throw new Exception("找不到该学年信息，可能已被删除");
			$condition = "1";
			if($_SERVER['REQUEST_METHOD'] == 'POST'){
				try{
					$data = array();
					//客户端提交方法
					$data['account'] = $postdate['account'];
					$data['password'] = md5($postdate['password']);
					$data['status'] = intval($postdata['status']);
					// if($result = $obj_school_week->spValidatoredit()->spValidator($data)) {
					// 	foreach($result as $item) {
					// 		throw new Exception($item[0]);
					// 	}
					// }
					// if(!$obj_user->checkEmail($data["regemail"], $id))
					// 	throw new Exception("该邮箱已被注册");
					// if(!$obj_user->checkTel($data["regtel"], $id))
					// 	throw new Exception("该联系电话已被注册");
					// unset($data['repassword']);
					// if($postdata['password']){
					// 	$data['password'] = md5($data['password']);
					// }else{
					// 	unset($data['password']);
					// }
					if(!$obj_student->update(array("id"=>$id), $data))
						throw new Exception("未知错误，学年信息更新失败");
					$url = spUrl("student", "datalist");
					$message = array('msg'=>'修改成功！','result'=>0, 'url'=>$url);
					echo json_encode($message);
					exit();
				}catch(Exception $e){
					$message = array('msg'=>$e->getMessage() ,'result'=>-1);
					echo json_encode($message);
					exit();
				}
			}
		}catch(Exception $e){
			$this->redirect(spUrl($this->controller, "datalist"), "非法操作！");
		}
		$this->id = $id;
		// $this->validator = $obj_user->getValidatoreditJS($id);
		$this->info_rs = $info_rs;
		$this->saveurl = spUrl($this->controller, "modify");
		$this->position_two = array("text"=>"学生信息修改", "url"=>spUrl($this->controller, "datalist"));
		$this->formtitle = array("text"=>"学生信息修改");
		$this->display($this->controller."/create.html");
	}

	public function delete(){
		$id = $this->spArgs('id');
		if($id){
			$obj_school_week = spClass('school_week');
			$url = spUrl($this->controller, "datalist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('id' => $v);
					// $user->delete($conditions);
					$obj_school_week->update($conditions, array('is_del' => 1));
				}
				echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('id' => $id);
				if($obj_school_week->update($conditions,array('is_del' =>1))){
					echo json_encode(array('msg'=>'删除成功！' , 'result'=>1, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'删除失败！' , 'result'=>0, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择删除项，再进行操作！' , 'result'=>0));
			exit();
		}
	}
	
	function usersdemo(){
		die("aa");
		$obj_demo = spClass('user_demo');
		$count = 1;
		$date = "2016-3-2";
		import("Rnd_China_Name.php");
		$obj_name = new Rnd_China_Name();
		for($i=0;$i<$count;$i++){
			$data = array();
			$data["realname"] = $obj_name->getName();
			$data["password"] = "96e79218965eb72c92a549dd5a330112";
			$data["regtel"] = $obj_demo->getRndTel();
			$data["regemail"] = $obj_demo->getRndMail($data);
			$data["sex"] = rand(0,2);
			$data["status"] = 1;
			$data["regtime"] = $obj_demo->getRandTime($date);
			if($id = $obj_demo->create($data))
				echo "create Success, id:$id <br/>";
			else
				echo "create Failed <br/>";
		}
	}
	
	function checkName(){
		@header('Cache-Control: no-cache, must-revalidate, max-age=0');
		@header('Pragma: no-cache');
		$obj_school_week = spClass('school_week');
		echo $obj_school_week->checkName($this->spArgs('<name></name>'), $this->spArgs('id'))?'true':'false';
		exit();
	}
	
	function modifystatus(){
		$id = $this->spArgs('id');
		$status = intval($this->spArgs("status"));
		$newrow = array("user_status"=>$status);
		if($id){
			$user = spClass('user');
			$url = spUrl("users", "userslist");
			if(is_array($id)){
				foreach ($id as $v){
					$conditions = array('uid' => $v);
					//$user->delete($conditions);
					$user->update($conditions, $newrow);
				}
				echo json_encode(array('msg'=>'更新成功！' , 'result'=>0, 'url'=>$url));
				exit();
			}
			else {
				$conditions = array('uid' => $id);
				if($user->update($conditions, $newrow)){
					echo json_encode(array('msg'=>'更新成功！' , 'result'=>0, 'url'=>$url));
					exit();
				}
				echo json_encode(array('msg'=>'更新失败！' , 'result'=>-1, 'url'=>$url));
				exit();
			}
		}else{
			echo json_encode(array('msg'=>'请先选择更新项，再进行操作！' , 'result'=>-2));
			exit();
		}
	}
}
?>